Skip to content

ext/intl: IntlDateFormatter::parse adds new optional argument. #13779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

devnexen
Copy link
Member

New option to update its internal calendar.

@devnexen devnexen force-pushed the intl_update_dateformat_parse branch 3 times, most recently from bacd94e to fbd4334 Compare March 21, 2024 23:16
@devnexen devnexen marked this pull request as ready for review March 22, 2024 08:09
@devnexen devnexen requested a review from kocsismate as a code owner March 22, 2024 08:09
@@ -154,10 +154,11 @@ public static function formatObject($datetime, $format = null, ?string $locale =

/**
* @param int $offset
* @param bool $updateCalendar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be added since it doesn't have more information than the param type declaration

function datefmt_parse(IntlDateFormatter $formatter, string $string, &$offset = null): int|float|false {}
/**
* @param int $offset
* @param bool $updateCalendar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

* @tentative-return-type
* @alias datefmt_parse
*/
public function parse(string $string, &$offset = null): int|float|false {}
public function parse(string $string, &$offset = null, bool $updateCalendar = false): int|float|false {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to bikeshed this much, but I'd still want to throw one more idea to think about:

public function parseToCalendar(string $string, &$offset = null): void {}

Maybe a signature like this would convey the message better that it is intended to modify the internal state. Let's wait for someone else's review and go with the signature which has more support :) I'm also fine with yours even though it's not my preference

Copy link
Member

@kocsismate kocsismate Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and I almost forgot: having a completely new method would also minimize the BC break, since IntlDateformatter::parse() is not final, so people may have been overriding it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid points, thanks. Will update later.

@kocsismate kocsismate requested a review from Girgias March 25, 2024 08:43
@devnexen devnexen force-pushed the intl_update_dateformat_parse branch from fbd4334 to 4f3af87 Compare March 25, 2024 18:43
@@ -377,6 +377,9 @@ function datefmt_format_object($datetime, $format = null, ?string $locale = null
/** @param int $offset */
function datefmt_parse(IntlDateFormatter $formatter, string $string, &$offset = null): int|float|false {}

/** @param int $offset */
function datefmt_parse_tocalendar(IntlDateFormatter $formatter, string $string, &$offset = null): int|float|false {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lately, the procedural interface is not extended anymore 🤔 See https://externals.io/message/114473#114673

@devnexen devnexen force-pushed the intl_update_dateformat_parse branch from 4f3af87 to 10aeb6d Compare March 25, 2024 21:30
@devnexen
Copy link
Member Author

ping :)

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this new method actually do?

if (UNEXPECTED(update_calendar)) {
UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if( text_utf16 ){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
if( text_utf16 ){
if (text_utf16) {

timestamp = ucal_getMillis( parsed_calendar, &INTL_DATA_ERROR_CODE(dfo));
} else {
timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if( text_utf16 ){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
if( text_utf16 ){
if (text_utf16) {

Comment on lines 171 to 173
char* text_to_parse = NULL;
size_t text_len =0;
zval* z_parse_pos = NULL;
int32_t parse_pos = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS: Either align them properly or don't bother having any spaces, but this is just weird

DATE_FORMAT_METHOD_INIT_VARS;

/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!",
if (zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!",

zval* z_parse_pos = NULL;
int32_t parse_pos = -1;

DATE_FORMAT_METHOD_INIT_VARS;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blerg I hate those sorts of macros.... but that's an existing problem with the codebase.

DATE_FORMAT_METHOD_INIT_VARS;

/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use zend_parse_method_parameters() when just adding a new method, as the getThis() is pointless and the first param being an object is also unnecessary.

}
}
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, true, return_value);
if(z_parse_pos) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
if(z_parse_pos) {
if (z_parse_pos) {

if (z_parse_pos) {
zend_long long_parse_pos;
ZVAL_DEREF(z_parse_pos);
long_parse_pos = zval_get_long(z_parse_pos);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new zval_try_get_long() API instead?

RETURN_FALSE;
}
parse_pos = (int32_t)long_parse_pos;
if((size_t)parse_pos > text_len) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
if((size_t)parse_pos > text_len) {
if ((size_t)parse_pos > text_len) {

RETURN_FALSE;
}
}
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, true, return_value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, true, return_value);
internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos ? &parse_pos : NULL, true, return_value);

@devnexen devnexen force-pushed the intl_update_dateformat_parse branch from 10aeb6d to f15e633 Compare April 25, 2024 06:26
RETURN_FALSE;
}
parse_pos = (int32_t)long_parse_pos;
if((size_t)parse_pos > text_len) {
if((size_t)parse_pos > ZSTR_LEN(text_to_parse)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are negative indexes supported?

Z_PARAM_ZVAL(z_parse_pos)
ZEND_PARSE_PARAMETERS_END();

object = getThis();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ZEND_THIS?

@devnexen devnexen force-pushed the intl_update_dateformat_parse branch from f15e633 to d69a34a Compare April 25, 2024 16:50
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor CS nits again, and adding test cases for the unhappy path

Comment on lines 46 to 48
UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if(text_utf16) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if(text_utf16) {
UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo));
udat_parseCalendar(DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if (text_utf16) {

Comment on lines 54 to 57
timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if(text_utf16) {
efree(text_utf16);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if(text_utf16) {
efree(text_utf16);
}
timestamp = udat_parse(DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo));
if (text_utf16) {
efree(text_utf16);
}

Comment on lines 171 to 173
zend_string *text_to_parse = NULL;
zval* z_parse_pos = NULL;
int32_t parse_pos = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't bother aligning it

Suggested change
zend_string *text_to_parse = NULL;
zval* z_parse_pos = NULL;
int32_t parse_pos = -1;
zend_string *text_to_parse = NULL;
zval* z_parse_pos = NULL;
int32_t parse_pos = -1;

RETURN_FALSE;
}
parse_pos = (int32_t)long_parse_pos;
if(parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: CS

Suggested change
if(parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) {
if (parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) {

Comment on lines 192 to 193
long_parse_pos = zval_try_get_long(z_parse_pos, &failed);
if (failed || ZEND_LONG_INT_OVFL(long_parse_pos)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the parsing fails, I'm pretty sure it always throws an Error so having another intl specific extension being thrown seems odd? Also a test for this case?

@devnexen devnexen force-pushed the intl_update_dateformat_parse branch from d69a34a to bc58213 Compare April 25, 2024 21:40
@@ -159,6 +159,12 @@ public static function formatObject($datetime, $format = null, ?string $locale =
*/
public function parse(string $string, &$offset = null): int|float|false {}

/**
* @param int $offset
* @tentative-return-type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the tentative return type, as it's a brand new method :)

@devnexen devnexen force-pushed the intl_update_dateformat_parse branch from bc58213 to 03d4443 Compare April 28, 2024 21:12
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final comment, but LGTM otherwise

bool failed = false;
long_parse_pos = zval_try_get_long(z_parse_pos, &failed);
if (failed) {
zend_argument_type_error(2, "invalid offset");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to standardize the error message

Suggested change
zend_argument_type_error(2, "invalid offset");
zend_argument_type_error(2, "must be of type int, %s given", zend_zval_value_name(z_parse_pos));

@devnexen devnexen force-pushed the intl_update_dateformat_parse branch from 03d4443 to ad4cfa9 Compare April 29, 2024 17:19
@devnexen devnexen closed this in 1cf4cc3 Apr 29, 2024
@iluuu1994
Copy link
Member

@iluuu1994
Copy link
Member

Nevermind, fixed here: 7f3fd30.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants